package com.farsoon.child.controller;


import com.farsoon.child.dto.UserReqDTO;
import com.farsoon.child.service.IUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/child/User")
@Api(value = "用户", tags = "用户")
public class UserConrtoller {

    @Resource

    IUserService mIUserService;
    private static final String XLSX = ".xlsx";
    private static final String XLS = ".xls";
    private String data = "Initialize Data";

    @ApiOperation(value = "Example", notes = "\n author : Ag1usee")
    @RequestMapping(value = "/Example", method = RequestMethod.POST)
    public void Example(@RequestBody Map<String, String> paramMap) {
        return;
    }

    @ApiOperation(value = "Test-测试接口", notes = " \n author: Ag1usee")
    @RequestMapping(value = "/Test", method = RequestMethod.GET)
    public String TestController() {
        System.out.println("TestController");
        mIUserService.saveOrUser();

        mIUserService.saveOrUser();
        System.out.println("TestController");
        return "TestController";
    }

    @ApiOperation(value = "test 定时器刷新接口", notes = "\n author: Ag1usee")
    @RequestMapping(value = "/RefreshData", method = RequestMethod.GET)
    public String RefreshData() {
        return data;
    }

    // 此注释保证在一个方法内多个定时任务在同一个方法中的定义和执行
    // @Schedules({
    //         @Scheduled(fixedRate = 5000),
    //         @Scheduled(cron = "0 0 12 * * ?")
    //     })

    // 每5秒执行一次  scheduled不应包含任何实参，和任何返回值
    // @Scheduled(fixedRate = 5000)
    public void refreshData321() {
        long currentTimeMillis = System.currentTimeMillis();
        Date date = new Date(currentTimeMillis);

        System.out.println("Date对象: " + date);

        // 当前时间戳（毫秒）: 1712631570952
        // 1631744400000L
        Date date1 = new Date();
        long timeInMillis = date1.getTime();
        System.out.println("毫秒时间戳: " + timeInMillis);

        // 创建SimpleDateFormat对象，并指定日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 格式化Date对象为指定格式的日期字符串
        String formattedDate = sdf.format(date);

        long timestamp = System.currentTimeMillis();
        System.out.println("当前时间戳（毫秒）: " + timestamp);

        // 获取当前日期时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("当前日期时间: " + currentDateTime);
        // 获取当前时间
        LocalTime currentTime = LocalTime.now();
        System.out.println("当前时间: " + currentTime);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        data = currentDateTime.format(formatter);

        // 解析日期时间字符串
        String dateTimeString = "2022-09-15 13:30:00";
        LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeString, formatter);
        System.out.println("解析后的日期时间: " + parsedDateTime);

        System.out.println("Refresh Data =>" + data);
    }

    // 定时任务每隔10秒刷新数据
    private ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

    public void RefreshController() {
        scheduledExecutorService.scheduleAtFixedRate(this::refreshDataAutomatically, 0, 10, TimeUnit.SECONDS);
    }

    private String refreshDataAutomatically() {
        // 模拟定时刷新数据的操作
        String data = "Auto-refreshed data at " + System.currentTimeMillis();
        return data;

    }

    @ApiOperation(value = "TestMultipartFile-测试接口", notes = " \n author: Ag1usee")
    @RequestMapping(value = "/TestMultipartFile", method = RequestMethod.POST)
    public void TestMultipartFile(UserReqDTO reqDTO, MultipartFile file) {
        String fileName = file.getOriginalFilename();
        Workbook book = null;
        try {
            if (fileName.endsWith(XLSX)) {
                book = new XSSFWorkbook(file.getInputStream());
            } else if (fileName.endsWith(XLS)) {
                POIFSFileSystem poifsFileSystem = new POIFSFileSystem(file.getInputStream());
                book = new HSSFWorkbook(poifsFileSystem);
            }
            Sheet sheet = book.getSheetAt(0);
//            for (Row row : sheet) {
//                for (Cell cell : row) {
//                    cell.setCellType(CellType.STRING);
//                    System.out.println(cell.getStringCellValue());
//                }
//            }
            Row row1 = sheet.getRow(0);
            Row row2 = sheet.getRow(1);
            // 读取每一列数据
            row1.getCell(2).setCellType(CellType.STRING);
            row2.getCell(2).setCellType(CellType.STRING);
            row1.getCell(6).setCellType(CellType.STRING);
            row2.getCell(6).setCellType(CellType.STRING);

            String OriginScaleX = row1.getCell(2).getStringCellValue();
            String OriginScaleY = row2.getCell(2).getStringCellValue();
            String ScaleX = row1.getCell(6).getStringCellValue();
            String ScaleY = row2.getCell(6).getStringCellValue();
            System.out.println(OriginScaleX + " " + OriginScaleY + " " + ScaleX + " " + ScaleY);
        } catch (IOException e) {
            e.printStackTrace();
        }
//      mIUserService.uploadFile(reqDTO,file);
    }

}
